<template>
  <div>
    <CustomTable
      ref="table"
      :showAlert="false"
      :showSelection="false"
      :columns="zzxxColumns"
      :loading="load"
      @change="onTableChange"
      :dataSource="pageData"
      :style="{ marginTop:'20px', height: '540px' }"
    >
    <!-- :scroll="{ y: 400 }" -->
      <template slot="xh" slot-scope="text, record, index">
        {{((pageFilter.pageNo - 1) * pageFilter.pageSize +(index + 1))>9?(pageFilter.pageNo - 1) * pageFilter.pageSize +(index + 1) :'0' + (index + 1) }}
      </template>
      <template slot="typeZzlb" slot-scope="text,record">
        <span v-if="record!=='' && record!==null">{{ dictText('资质类别',text) }}</span>
      </template>
      <template slot="level" slot-scope="text,record">
        <span v-if="record!=='' && record!==null">{{ dictText('资质级别',text) }}</span>
      </template>
      <template slot="dateFirsthdzz" slot-scope="record">
        <span v-if="record!=='' && record!==null">{{ moment(record).format("YYYY-MM-DD") }}</span>
      </template>
      <template slot="effictivedateto" slot-scope="record">
        <span v-if="record!=='' && record!==null">{{ moment(record).format("YYYY-MM-DD") }}</span>
      </template>
      <template slot="fileAction" slot-scope="text, record">
        <span v-if="record.file==null || record.file==''"></span>
        <template v-else v-for="(item,index) in JSON.parse(record.file)">
          <div :key="index" style="margin-bottom:5px;display:flex" >
            <div style="width: 85%;">{{item.name}}</div>  <a @click="downloadPDf(item)" >下载</a>
           </div>
        </template>
      </template>
    </CustomTable>
  </div>
</template>

<script>
import api from '@/api/index'
import download from 'downloadjs'; // 引用插件
export default {
  props: {
    codeZzjg: {
      type: String
    }
  },
  data() {
    return {
      load:false,
      zzxxColumns: [
          {
              title: '序号',
              dataIndex: 'xh',
              key: 'xh',
              width:80,
              scopedSlots: { customRender: "xh" }
            },
            {
              title: '资质类别',
              dataIndex: 'typeZzlb',
              key: 'typeZzlb',
              scopedSlots: { customRender: "typeZzlb" }
            },
            {
              title: '级别',
              dataIndex: 'level',
              key: 'level',
              scopedSlots: { customRender: "level" }
            },
            {
              title: '证书编号',
              dataIndex: 'idZs',
              key: 'idZs',
              ellipsis: true,
            },
            {
              title: '附件',
              dataIndex: 'fileAction',
              key: 'file',
              width:250,
              scopedSlots: { customRender: "fileAction" }
            },
            {
              title: '取得资质时间',
              dataIndex: 'dateFirsthdzz',
              key: 'dateFirsthdzz',
              ellipsis: true,
              scopedSlots: { customRender: "dateFirsthdzz" }
            },
            {
              title: '有效期至',
              dataIndex: 'effictivedateto',
              key: 'effictivedateto',
              ellipsis: true,
              scopedSlots: { customRender: "effictivedateto" }
            },
            {
              title: '核发机关',
              dataIndex: 'fzjg',
              key: 'fzjg',
              ellipsis: true,
            },
        ],
      pageData: [],
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
        isPass:true,
      },
      dictDW:undefined
    };
  },
  created() {
    this.pageFilter.codeZzjg = this.codeZzjg;
    this.refreshTable();
    this.dictList()
  },
  methods: {
      //字典获取
      dictList(){
        api.get_dict().then(ser => {
          this.dictDW = ser
          console.log(this.dictDW)
        })
      },
      dictText(val,text){
        let list = []
        var arr = (text || '').split(`,`)
        console.log(arr)
        for(let item of arr){
          for(let index of this.dictDW){
            if(item === index.dictValue && val === index.dictType){
              list.push(index.dictLabel)
            }
          }
        }
        if(list.length !== 0){

          return list.toString()
        } else {
          return text
        }
      },
      onTableChange(pageFilter) {
        this.pageFilter = { ...this.pageFilter, ...pageFilter };
        this.refreshTable();
      },
      async refreshTable() {
        this.load = true;
        await api.get_unitZzxx_list(this.pageFilter) .then(res => {
          let pageNo = this.pageFilter.pageNo;
          let pageSize = this.pageFilter.pageSize;
          let records = res.rows;
          let total = res.total;
          this.pageData = {
            ...this.pageData,
            pageNo,
            records,
            pageSize,
            total
          };
          this.load = false;

        })
        .catch(err => {
          this.load = false;
        });


      },
      downloadPDf(item){
        download("/api"+item.path);
      }
    }
};
</script>

<style lang="less">
</style>
